package com.gxa.jd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.jd.mapper.OrderItemMapper;
import com.gxa.jd.pojo.GoodsOrderItem;
import com.gxa.jd.service.OrderItemService;
import com.gxa.entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class OrderItemServiceImpl implements OrderItemService {

    @Autowired
    private OrderItemMapper orderItemMapper;

    /**
     * 返回全部记录
     *
     * @return
     */
    @Override
    public List<GoodsOrderItem> findAll() {
        return orderItemMapper.selectAll();
    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    @Override
    public PageResult<GoodsOrderItem> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        Page<GoodsOrderItem> orderItems = (Page<GoodsOrderItem>) orderItemMapper.selectAll();
        return new PageResult<GoodsOrderItem>(orderItems.getTotal(), orderItems.getResult());
    }

    /**
     * 条件查询
     *
     * @param searchMap 查询条件
     * @return
     */
    @Override
    public List<GoodsOrderItem> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return orderItemMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageResult<GoodsOrderItem> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        Page<GoodsOrderItem> orderItems = (Page<GoodsOrderItem>) orderItemMapper.selectByExample(example);
        return new PageResult<GoodsOrderItem>(orderItems.getTotal(), orderItems.getResult());
    }

    /**
     * 根据Id查询
     *
     * @param id
     * @return
     */
    @Override
    public GoodsOrderItem findById(Long id) {
        return orderItemMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     *
     * @param orderItem
     */
    @Override
    public void add(GoodsOrderItem orderItem) {
        orderItemMapper.insert(orderItem);
    }

    /**
     * 修改
     *
     * @param orderItem
     */
    @Override
    public void update(GoodsOrderItem orderItem) {
        orderItemMapper.updateByPrimaryKeySelective(orderItem);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        orderItemMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(GoodsOrderItem.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // image
            if (searchMap.get("image") != null && !"".equals(searchMap.get("image"))) {
                criteria.andLike("image", "%" + searchMap.get("image") + "%");
            }
            // name
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            // order_id
            if (searchMap.get("orderId") != null && !"".equals(searchMap.get("orderId"))) {
                criteria.andLike("orderId", "%" + searchMap.get("orderId") + "%");
            }
            // sku_id
            if (searchMap.get("skuId") != null && !"".equals(searchMap.get("skuId"))) {
                criteria.andLike("skuId", "%" + searchMap.get("skuId") + "%");
            }

            // num
            if (searchMap.get("num") != null) {
                criteria.andEqualTo("num", searchMap.get("num"));
            }

        }
        return example;
    }

}
